package posomasterworker;


import mpi.*;

/* Class Worker */
public class POSOWorker{
	private int id;
	
	/* Constructor */
	public POSOWorker(int id_){
		id = id_;
	}
	public int getID(){
		return id;
	}
	/* Receive data type DOUBLE */
	public void receiveData(double[] data, int offset, int count, Datatype type, int source, int tag){
		MPI.COMM_WORLD.Recv((Object)data, offset, count, type, source, tag);
	}
	
	/* Receive data type INT */
	public void receiveData(int[] data, int offset, int count, Datatype type, int source, int tag){
		MPI.COMM_WORLD.Recv((Object)data, offset, count, type, source, tag);
	}
	
	/* Send data type INT */
	public void sendData(int[] data, int offset, int count, Datatype type, int dest, int tag ){
		MPI.COMM_WORLD.Send((Object)data, offset, count, type, dest, tag);
	}
	
	/* Send data type DOUBLE */
	public void sendData(double[] data, int offset, int count, Datatype type, int dest, int tag){
		MPI.COMM_WORLD.Send((Object)data, offset, count, type, dest, tag);
	}
	
	/* Do work */
	public double[] doWork(int[] n, double[] a, double[] b){
		/* Work on data */
		int ia = 0;
		int ie = n[0]/2;
		
		double rmin = a[ia];
		double rmax = rmin;
		  
		for(int i = 0; i < ie; i++){
			if(a[i] < rmin) rmin = a[i];
			if(a[i] > rmax) rmax = a[i];
		}
		b[0] = rmin;
		b[1] = rmax;
		
		return b;
	}
}
